package cn.xiangxu.telecom.login.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.xiangxu.telecom.login.entity.Admin;
import cn.xiangxu.telecom.login.service.AdminService;
import cn.xiangxu.telecom.login.service.ApplicationException;

@Controller
public class LoginController {
	@Resource(name="adminService")
	private AdminService as;
	
	@RequestMapping("/toLogin.do")
	public String toLogin() {
		System.out.println("去往登录页面...");
		return "main/login";
	}
	
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request) {
		String adminCode = request.getParameter("adminCode");
		String password = request.getParameter("password");
		
		if(null != adminCode && null != password) {
			try {
				Admin admin = as.checkAdminInfo(adminCode, password);
			} catch(Exception e) {
				e.printStackTrace();
				if(e instanceof ApplicationException) {
					request.setAttribute("fail", e.getMessage());
					return "main/login";
				} 
				return "exception/error";
			}
		} else {
			System.out.println("有的请求参数没有读取到");
			return "main/login";
		}
		
		return "redirect:toIndex.do";
	}
	
	@RequestMapping("/toIndex.do")
	public String toIndex() {
		System.out.println("前往主页面...");
		return "main/index";
	}
}
